package com.lcifn.spring.transaction;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

public class ProgramTransactionDemo {
	
	private DataSource ds;

	@Test
	public void testTransaction(){
		PlatformTransactionManager tm = new DataSourceTransactionManager(ds);
		
		TransactionDefinition td = new DefaultTransactionDefinition();
		TransactionStatus status = tm.getTransaction(td);
		
		try {
			// do business transaction
		} catch (Exception e) {
			tm.rollback(status);
			throw e;
		}
		
		tm.commit(status);
	}
}
